﻿using EquipmentAlarmSystem.Models;
using EquipmentAlarmSystem.Service.Library;
using EquipmentAlarmSystem.Service.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace EquipmentAlarmSystem.Controllers;

public class DeviceManagementController : Controller
{
    [Authorize]
    public IActionResult AddSiemensS7Device([FromForm] DeviceInformationSiemensS7 device)
    {
        if (device.Id == null && device.Name == null)
        {
            var data = Basics.GetID();

            NewDeviceId newDeviceId = new() { Id = 10000 };

            if (data is not null)
            {
                newDeviceId.Id = int.Parse(data[^1]!.ToString()!)! + 1;
            }
            return View(newDeviceId);
        }
        else
        {
            NewDeviceId newDeviceId = new() { Id = int.Parse(device.Id) + 1 };
            newDeviceId.Content = Basics.ModifEquipmentConfiguration(device);
            return View(newDeviceId);
        }
    }

    [Authorize]
    public IActionResult AddModusTcpDevice([FromForm] DeviceInformationModusTcp device)
    {
        if (device.Id == null && device.Name == null)
        {
            var data = Basics.GetID();

            NewDeviceId newDeviceId = new() { Id = 10000 };

            if (data is not null)
            {
                newDeviceId.Id = int.Parse(data[^1]!.ToString()!)! + 1;
            }
            return View(newDeviceId);
        }
        else
        {
            NewDeviceId newDeviceId = new() { Id = int.Parse(device.Id) + 1 };
            newDeviceId.Content = Basics.ModifEquipmentConfiguration(device);
            return View(newDeviceId);
        }
    }
}
